<template>
  <div
    id="editor"
    class="editor"
  >
    <div
      id="div1"
      class="toolbar"
    />
    <div class="text-container">
      <div
        id="div2"
        ref="text"
        class="text"
      />
      <div
        id="div3"
        ref="text2"
        class="text2"
      >
        <div
          id="ARTICLE"
          ref="ARTICLE"
          class="article"
          name="ARTICLE"
          v-html="html"
        />
      </div>
    </div>
  </div>
</template>

<script>
import editor from 'wangeditor'
export default {
  name: 'Editor',
  props: {},
  data() {
    return {
      editor: null,
      html: null
    }
  },
  watch: {
    isEdit(val) {
      this.editor.$textElem.attr('contenteditable', val)
    }
  },
  mounted() {
    this.initEditor()
    this.$refs.text.addEventListener('scroll', this.onScroll, true)
    // window.addEventListener('scroll', this.throttle(this.onScroll, 1000), true)
    // window.addEventListener('scroll', this.onScroll, true)
  },
  beforeDestroy() {
    this.$refs.text.removeEventListener('scroll', this.onScroll)
    // window.removeEventListener('scroll', this.onScroll)
  },
  methods: {
    throttle(fn, wait) {
      var timer = null
      return function () {
        var context = this
        var args = arguments
        if (!timer) {
          timer = setTimeout(() => {
            fn.apply(context, args)
            timer = null
          }, wait)
        }
      }
    },

    onScroll() {
      let x = document.getElementsByClassName('w-e-text')
      this.$refs.ARTICLE.scrollTop = x[0].scrollTop
    },
    setHtml(html) {
      this.html = html
    },
    clearCon() {
      this.editor.txt.clear()
    },
    setContent(content) {
      this.editor.txt.html(content)
    },
    getContent() {
      if (this.editor) return this.editor.txt.html()
      return ''
    },
    initEditor() {
      let Editor = new editor('#div1', '#div2')
      Editor.customConfig.uploadImgShowBase64 = true
      Editor.customConfig.onchange = (html) => {
        this.$emit('change', html)
      }
      Editor.customConfig.zIndex = 2
      this.editor = Editor
      this.editor.create()
    }
  }
}
</script>

<style scoped lang="scss">
.editor {
  display: flex;
  flex-direction: column;
  // border: 1px solid #dddddd;
  // border-radius: 40px;
  width: 100%;
  /* overflow: hidden;

  margin-left: 2%;
  position: relative;
  height: calc(100% - 80px) !important;
  display: flex; */
}

.toolbar {
  flex-wrap: wrap;
  // overflow: hidden;
  width: 100%;
  border: 1px solid #ddd;
  background-color: #f9f9f9;
}

.text-container {
  position: relative;
  display: flex;
  flex: 1;
  border-bottom: 1px solid #ddd;
  border-left: 1px solid #ddd;
  border-right: 1px solid #ddd;

  .text {
    display: flex;
    width: 50%;
    height: 100%;
    word-wrap: break-word;
    word-break: break-all;
    background-color: white;
  }

  .text2 {
    display: flex;
    width: 50%;
    height: 100%;
    border-left: 1px solid #ddd;
    background-color: #f9f9f9;

    .article {
      width: 100%;
      padding-right: 10px;
      padding-left: 10px;
      overflow-y: scroll;
      word-wrap: break-word;
      word-break: break-all;
    }
  }
}
</style>